package com.franferri;

import java.io.IOException;
import java.io.Reader;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

/**
 * @author <a href="mailto:franferri@gmail.com">Francisco Ferri &lt;franferri@gmail.com&gt;</a>
 * @since November 22 2010
 * 
 * @see <p>
 *      Detailed info at <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/text/html/HTMLEditorKit.html">HTMLEditorKit</a>
 *      </p>
 */

public class Html2Text extends HTMLEditorKit.ParserCallback {

	StringBuffer tmp_text;

	/**
	 * Parse the input text and process it, we are ignoring the input charset.
	 * 
	 * @param in
	 * @throws IOException
	 */
	public void parse(Reader in) throws IOException {

		tmp_text = new StringBuffer();

		ParserDelegator delegator = new ParserDelegator();

		delegator.parse(in, this, Boolean.TRUE);

	}

	/**
	 * Parse the input text and process it.
	 * 
	 * @param in
	 * @throws IOException
	 */
	public void parse(Reader in, boolean ignoreCharset) throws IOException {

		tmp_text = new StringBuffer();

		ParserDelegator delegator = new ParserDelegator();

		delegator.parse(in, this, ignoreCharset);

	}

	public void handleText(char[] text, int pos) {
		tmp_text.append(text);
	}

	/**
	 * Get the procesed text.
	 * 
	 * @return
	 */

	public String getText() {
		return tmp_text.toString();
	}

}
